package com.object.module.lq.sys.controller;

import com.alibaba.excel.EasyExcel;
import com.object.module.lq.sys.entity.TUserEntity;
import com.object.module.lq.sys.ov.TUserAndAddUser;
import com.object.module.lq.sys.service.TUserService;
import com.object.utils.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author 小🌤
 * @email 1578442339@qq.com
 * @date 2021-07-24 15:24:06
 */
@RestController
@RequestMapping("/user1")
public class TUserController1 {
    @Autowired
    private TUserService userService;

    /**
     * 列表
     */
    @ApiOperation("列表")
    @RequestMapping("/list")
    public Q<List<TUserEntity>> list(@ApiParam(value = "查询用户列表", required = true) @RequestParam(required = false) Map<String, Object> params) {
        return userService.listPage(params, true);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    public Q<Boolean> save(@RequestBody TUserEntity user) {
        user.setUrPassword(PasswordEncryp.getPaawordEncryp(user.getUrPassword()));
        userService.save(user);
        return new Q<Boolean>().ok(true);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Q<Boolean> update(@RequestBody TUserEntity user) {
        userService.updateById(user);
        return new Q<Boolean>().ok(true);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Q<Boolean> delete(@RequestBody Integer[] urIds) {
        userService.removeByIds(Arrays.asList(urIds));
        return new Q<Boolean>().ok(true);
    }

    /**
     * 导出
     *
     * @param params
     * @throws IOException
     */
    @GetMapping("export")
    public void importExcel(@ApiParam(value = "导出条件", required = true) @RequestParam(required = false) Map<String, Object> params, HttpServletResponse response) throws IOException {
        Q<List<TUserEntity>> list = userService.listPage(params, false);
        // 设置响应头
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        String fileName = URLEncoder.encode("用户信息.xlsx", "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename="+fileName);
        EasyExcel.write(response.getOutputStream(), TUserEntity.class).sheet("用户信息").doWrite(list.getT());
    }
}
